package designpattern.adapter;

/**
 * 提供给应用层的接口封装（有几种实现方式）
 */
public class AdapterPattern {

    //-----------提供给应用层的接口（代码）-----不同的方式---------------start-------

    /**
     * Approach 1
     */
    class WhatIUse {
        public void op(WhatIWant iWant) {
            iWant.f();
        }
    }

    /**
     * Approach 2: build adapter use into op():
     */
    class WhatIUse2 extends WhatIUse{
        public void op(WhatIHave iHave){
            new ProxyAdapter(iHave).f();
        }
    }

    /**
     *  Approach 3: build adapter into WhatIHave:
     */
    class WhatIHave2 extends WhatIHave implements WhatIWant{

        @Override
        public void f() {
            g();
            h();
        }
    }

    /**
     * Approach 4: use an inner class:
     */
    class WhatIHave3 extends WhatIHave {
        private class InnerAdapter implements WhatIWant{
            public void f() {
                g();
                h();
            }
        }
        public WhatIWant whatIWant() {
            return new InnerAdapter();
        }
    }


    //-----------提供给应用层的接口（代码）-----不同的方式---------------end-------



    //测试

    WhatIUse whatIUse = new WhatIUse();
    WhatIHave whatIHave = new WhatIHave();
    WhatIWant adapt= new ProxyAdapter(whatIHave);

    WhatIUse2 whatIUse2 = new WhatIUse2();
    WhatIHave2 whatIHave2 = new WhatIHave2();
    WhatIHave3 whatIHave3 = new WhatIHave3();

    public void test() {
        // Approach 1:传入适配器
        whatIUse.op(adapt);
        // Approach 2:与实现一类似，只是内部使用适配器，应用层只需传入已有的对象
        whatIUse2.op(whatIHave);
        // Approach 3:实现一的变形（实现一是object adapter,实现三是class adapter）
        whatIUse.op(whatIHave2);
        // Approach 4:利用内部类创建适配器并返回，再由应用层决定调用适配器的哪种方法
        whatIUse.op(whatIHave3.whatIWant());
    }

    public static void main(String[] args) {
        new AdapterPattern().test();
    }


}
